﻿using System.Collections.Generic;

namespace Uber.Data
{
    using System.Data;
    using System.Data.SqlClient;
    using Core.Model;
    using Core.Repository;

    public class GoldBuyRepo : BaseRepo, IGoldBuyRepo
    {
        public GoldBuyRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public string Insert(GoldBuy o)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "insertGoldBuy";
                    cmd.Parameters.Add("amount", SqlDbType.Decimal).Value = o.Amount;
                    cmd.Parameters.Add("quantity", SqlDbType.Int).Value = o.Quantity;
                    cmd.Parameters.Add("tid", SqlDbType.NVarChar, 30).Value = o.Tid;
                    cmd.Parameters.Add("timp", SqlDbType.DateTime).Value = o.Timp;
                    cmd.Parameters.Add("nid", SqlDbType.BigInt, 1).Value = o.Nid;

                    conn.Open();

                    return (string) cmd.ExecuteScalar();
                }
            }
        }

        public int Count(string tid)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "countGoldBuyByTid";
                    cmd.Parameters.Add("tid", SqlDbType.NVarChar, 30).Value = tid;
                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }

        public int Count()
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getGoldBuyCount";
                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }

        public IEnumerable<GoldBuy> GetPage(int page, int pageSize)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getGoldBuy";
                    cmd.Parameters.Add("pageSize", SqlDbType.Int).Value = pageSize;
                    cmd.Parameters.Add("page", SqlDbType.Int).Value = page;
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new GoldBuy();
                            o.Id = dr.GetInt64(0);
                            o.Amount = dr.GetDecimal(1);
                            o.Quantity = dr.GetInt32(2);
                            o.Tid = dr.GetString(3);
                            o.Timp = dr.GetDateTime(4);
                            o.Nid = dr.GetInt64(5);
                            o.Name = dr.GetString(6);
                            yield return o;
                        }
                    }
                }
            }
        }

    }
}